<?php

namespace App\Controllers\Diary;

use App\Models\Diary\Topic;
use App\Models\Diary\Diary;

class GetLastActivity extends \App\Libs\Base\BaseAction
{

    protected function beforeRun()
    {
        parent::beforeRun();
        //$this->needLogin = TRUE;
    }

    /**
     * @return array 返回往期活动的数据；
     *      querystring： 页码page,需要的数量num；page（默认返回第一页）,num(默认10),
     */
    public function execute()
    {
        $getPage = $this->request->get('page', 'int', 1);
        $getNumPerPage = $this->request->get('num', 'int', 10);
        $offset = ($getPage - 1) * $getNumPerPage;
        //获取可以显示的期数中，最新一期的数值；
        $topicModel = new Topic();
        $period = $topicModel->getNewlestPeriodNum();
        //查询数据库得到指定的页，指定的条，数据。。。
        $allShowPeriods = Topic::findAll([
            'conditions' => 'isshow = ?1 and period < ?2 ',
            'order' => 'period desc',
            'offset' => $offset,
            'limit' => $getNumPerPage,
            //返回周期数，不使用id
            'columns' => 'period as id,period,topic_title,topic_logo,topic_sum_comment',
            'bind' => array(1 => true, 2 => $period)
        ])->toArray();

        if (empty($allShowPeriods)) {
            return \App\Libs\Views\Json::view(0, '还有有开启任何活动', []);
        } else {
            return \App\Libs\Views\Json::view(0, '成功', $allShowPeriods);
        }
    }
}
